package ynu.edu.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
@RefreshScope
public class ConsumerController {

    @LoadBalanced
    private final RestTemplate restTemplate;

    // 从配置中心读取配置
    @Value("${app.message:Default Consumer Message}")
    private String configMessage;

    @Value("${app.version:1.0.0}")
    private String version;

    public ConsumerController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/call")
    public String callService() {
        String response = restTemplate.getForObject("http://provider/provide", String.class);
        return "Consumer1 :" + response;
    }

    @GetMapping("/config")
    public String getConfig() {
        return "Consumer1 Config - Message: " + configMessage + ", Version: " + version;
    }

}
